/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseParameterDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseParameterDao : BaseDao
    {
        public BaseParameterDao()
        {
            base.CurrentTableName = BaseParameterTable.TableName;
        }

        public BaseParameterDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseParameterDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseParameterEntity parameterEntity)
        {
            string str = string.Empty;
            string[] names = new string[3];
            object[] values = new object[3];
            names[0] = BaseParameterTable.FieldCategoryID;
            names[1] = BaseParameterTable.FieldParameterID;
            names[2] = BaseParameterTable.FieldParameterCode;
            values[0] = parameterEntity.CategoryID;
            values[1] = parameterEntity.ParameterID;
            values[2] = parameterEntity.ParameterCode;
            if (this.Exists(names, values))
            {
                base.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            str = this.AddEntity(parameterEntity);
            base.ReturnStatusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string Add(string categoryID, string parameterID, string parameterCode, string parameterContent, bool worked, bool enabled)
        {
            BaseParameterEntity parameterEntity = new BaseParameterEntity
            {
                CategoryID = categoryID,
                ParameterID = parameterID,
                ParameterCode = parameterCode,
                ParameterContent = parameterContent,
                Worked = worked,
                Enabled = enabled
            };
            return this.Add(parameterEntity);
        }

        protected string AddEntity(BaseParameterEntity parameterEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseParameterTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseParameterTable.TableName);
            builder.SetValue(BaseParameterTable.FieldID, sequence);
            builder.SetValue(BaseParameterTable.FieldCategoryID, parameterEntity.CategoryID);
            builder.SetValue(BaseParameterTable.FieldParameterID, parameterEntity.ParameterID);
            builder.SetValue(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode);
            builder.SetValue(BaseParameterTable.FieldWorked, parameterEntity.Worked ? 1 : 0);
            builder.SetValue(BaseParameterTable.FieldParameterContent, parameterEntity.ParameterContent);
            builder.SetValue(BaseParameterTable.FieldEnabled, parameterEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseParameterTable.FieldDescription, parameterEntity.Description);
            builder.SetValue(BaseParameterTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetValue(BaseParameterTable.FieldCreateUserRealname, base.UserInfo.Realname);
            builder.SetValue(BaseParameterTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseParameterTable.FieldCreateDate);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public int DeleteByParameter(string categoryID, string parameterID)
        {
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID };
            object[] values = new object[] { categoryID, parameterID };
            return this.BatchDelete(names, values);
        }

        public int DeleteByParameterCode(string categoryID, string parameterID, string parameterCode)
        {
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID, BaseParameterTable.FieldParameterCode };
            object[] values = new object[] { categoryID, parameterID, parameterCode };
            return this.BatchDelete(names, values);
        }

        public DataTable GetDTByParameter(string categoryID, string parameterID)
        {
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID };
            object[] values = new object[] { categoryID, parameterID };
            return this.GetDT(names, values);
        }

        public DataTable GetDTParameterCode(string categoryID, string parameterID, string parameterCode)
        {
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID, BaseParameterTable.FieldParameterCode };
            object[] values = new object[] { categoryID, parameterID, parameterCode };
            return this.GetDT(names, values);
        }

        public string GetParameter(string categoryID, string parameterID, string parameterCode)
        {
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID, BaseParameterTable.FieldParameterCode };
            object[] values = new object[] { categoryID, parameterID, parameterCode };
            return this.GetProperty(names, values, BaseParameterTable.FieldParameterContent).ToString();
        }

        public int SetParameter(string categoryID, string parameterID, string parameterCode, string parameterContent)
        {
            int num = 0;
            string[] names = new string[] { BaseParameterTable.FieldCategoryID, BaseParameterTable.FieldParameterID, BaseParameterTable.FieldParameterCode };
            object[] values = new object[] { categoryID, parameterID, parameterCode };
            if ((parameterContent == null) || (parameterContent.Length == 0))
            {
                return this.BatchDelete(names, values);
            }
            num = this.SetProperty(names, values, BaseParameterTable.FieldParameterContent, parameterContent);
            if (num == 0)
            {
                BaseParameterEntity parameterEntity = new BaseParameterEntity
                {
                    CategoryID = categoryID,
                    ParameterID = parameterID,
                    ParameterCode = parameterCode,
                    ParameterContent = parameterContent
                };
                this.AddEntity(parameterEntity);
                num = 1;
            }
            return num;
        }

        public int Update(BaseParameterEntity parameterEntity)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseParameterTable.TableName, parameterEntity.ID, parameterEntity.ModifyUserID, parameterEntity.ModifyDate))
            {
                base.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode, parameterEntity.ID))
            {
                base.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            num = this.UpdateEntity(parameterEntity);
            if (num == 1)
            {
                base.ReturnStatusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            base.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        protected int UpdateEntity(BaseParameterEntity parameterEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseParameterTable.TableName);
            builder.SetValue(BaseParameterTable.FieldCategoryID, parameterEntity.CategoryID);
            builder.SetValue(BaseParameterTable.FieldParameterCode, parameterEntity.ParameterCode);
            builder.SetValue(BaseParameterTable.FieldParameterID, parameterEntity.ParameterID);
            builder.SetValue(BaseParameterTable.FieldParameterContent, parameterEntity.ParameterContent);
            builder.SetValue(BaseParameterTable.FieldWorked, parameterEntity.Worked ? 1 : 0);
            builder.SetValue(BaseParameterTable.FieldEnabled, parameterEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseParameterTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetValue(BaseParameterTable.FieldModifyUserRealname, base.UserInfo.Realname);
            builder.SetDBNow(BaseParameterTable.FieldModifyDate);
            builder.SetWhere(BaseParameterTable.FieldID, parameterEntity.ID);
            return builder.EndUpdate();
        }
    }
}